#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
@Time        : 2021/11/4 15:59
@Author      : Albert Darren
@Contact     : 2563491540@qq.com
@File        : holes_morphology.py
@Version     : Version 1.0.0
@Description : TODO
@Created By  : PyCharm
"""
from skimage.io import imread
from DIP_experiment_5.util import contrast_show
from skimage.morphology import remove_small_holes, remove_small_objects, label
import numpy as np

FONT_PATH = "C:/Windows/Fonts/simhei.ttf"
IM_PATH = "../experiment_fig/holes.png"  # RGBA
im = imread(IM_PATH, as_gray=True)  # 读入为greyscale image
im = im.astype(np.bool_)
fill_holes_im = remove_small_holes(im, area_threshold=1000)
fill_objects_im = remove_small_objects(fill_holes_im, min_size=5000)
_, connected_region_num = label(fill_objects_im, return_num=True)
print("去除像素数小于5000的圆形后连通分量数目:{}".format(connected_region_num))
im_dict = {"原始图像": im, "填充孔洞图像": fill_holes_im, "去除pixel_num<5000圆形后图像": fill_objects_im}
contrast_show(im_dict, (1, 3), wspace=0.3, font=FONT_PATH)
